/*-----------------------------------------------------------------
 *
 * prim_node.h
 *   Definitions for "primitive" node types, those that are used in
 *   more than one of the parse/plan/execute stages of the query.
 *   Currently these are mostly nodes for executable expressions.
 *
 *-----------------------------------------------------------------
 */
#ifndef PRIM_NODE_H
#define PRIM_NODE_H

#include "access/attnum.h"
#include "op_list.h"

/*-----------------------------------------------------------------
 *  node definitions
 *-----------------------------------------------------------------
 */

/*
 * Alias - 
 *   specifies an alias for a range variable; the alias might also
 *   specify renaming of columns within the table.
 * 
 * Note: colnames is a list of Value nodes (always strings). In Alias structs
 * associated with RTEs, there may be entries corresponding to dropped columns;
 * these are nomally empty strings("").
 */
/* _line_0038 */
typedef struct Alias {
  NodeTag type;
  char *aliasname;  /* aliased rel name (never qualified) */
  List *colnames;  /* optional list of column aliases */
} Alias;

typedef enum InhOption
{
	INH_NO,						/* Do NOT scan child tables */
	INH_YES,					/* DO scan child tables */
	INH_DEFAULT					/* Use current SQL_inheritance option */
} InhOption;

/* RangeVar - range variable, used in FROM clause
 * Also used to represent table name in utility statement;
 * there, the alias field is not used, and ingOpt shows whether
 * to apply the operation recursively to chid tables.
 * In some contexts it is also useful ti carry a TEMP table indication here.
 */
 /* _line_0069 */
typedef struct RangeVar {
  NodeTag type;
  char *catalogname;  /* the catalog(database) name, or NULL */
  char *schemaname;  /* the schema name, or NULL */
  char *relname;  /* the relation/sequence name */
  InhOption inhOpt;  /* expand rel by inheritance? recursively act
		      * on children? */
  bool istemp;  /* is this a temp relation/sequence? */
  Alias *alias;  /* table alias & optional column aliases */
  int location;  /* token location, or -1 if unknown */
}RangeVar;

/*-----------------------------------------------------------------
 * node types for executable expressions
 *-----------------------------------------------------------------
 */

/* Expr - generic superclass for executable-expression nodes
 * 
 * All node types that are used in executable expression trees should derive
 * from Expr (that is, have Expr as their first field). Since Expr only contains
 * NodeTag, this is a formality, but it is an easy form of documentation.
 */
/* _line_0109 */
typedef struct Expr {
  NodeTag type;
}Expr;

/* Const */
/* _line_0153 */
typedef struct Const {
  Expr xpr;
  Oid consttype;  /* op_type OID of the constant's datatype */
  int32 consttypmod;  /* typmod value, if any */
  int constlen;  /* typlen of the constant's datatype */
  Datum constvalue;  /* the constant's value */
  bool constisnull;  /* whether the constant is null (if true,
  * constvalue is undefined) */
  bool constbyval;  /* whether this datatype is passed by value. 
  * If true, then all the information is stored in the Datum. If
  * false, then the Datum contains a pointer to the information */
  int location;  /* token location, or -1 if unknown */
}Const;

/*
 * BoolExpr - expression node for the basic Boolean operators AND|OR|NOT
 *
 * Notice the arguments are given as a list, For NOT, of course the list
 * must always have exactly one element. For AND and OR, the executer can 
 * handle any number of arguments. The parser generally treats AND and OR
 * as bunary and so it typically only produce two-elements lists, but the 
 * optimizer will flatten trees of AND and OR nodes to produce longer lists
 * when possible. There are also a few special cases where more arguments can
 * appear before optimization.
 */
/* _line_0379 */
typedef enum BoolExprType {
  AND_EXPR, OR_EXPR, NOT_EXPR
}BoolExprType;

typedef struct BoolExpr {
  Expr xpr;
  BoolExprType boolop;
  List *args;  /* arguments to this expression */
  int location; /* token location, or -1 if uknown */
}BoolExpr;

/*-------------------------------------
 * NullTest
 *   represents the operation of testing a value for NULLness.
 *   The appropriate test is performed and returned as a boolean Datum.
 *-------------------------------------
 */
 /* _line_0909 */
typedef enum NullTestType {
  IS_NULL, IS_NOT_NULL
}NullTestType;

typedef struct NullTest {
  Expr xpr;
  Expr *arg;  /* input expression */
  NullTestType nulltesttype;
} NullTest;

/*
 * Placeholder node for a DEFAULT marker in an INSERT or UPDATE command.
 *
 * This is not an executable expression: it must be replaced by the actual
 * column default expression during rewriting.	But it is convenient to
 * treat it as an expression node during parsing and rewriting.
 */
/* _line_0985 */
typedef struct SetToDefault
{
  Expr xpr;
  Oid typeId;  /* type for substituted value */
  int32 typeMod;  /* typemod for substituted value */
  int location;  /* token location, or -1 if unknown */
} SetToDefault;

/*-------------------------------------
 * TargetEntry - 
 *   a target entry (used in query target lists)
 *
 * Strictly speaking, a TargetEntry isn't an expression node (since it can't
 * be evaluated by ExecEvalExpr). But we treat it as one anyway, since in very
 * many places it's convenient to process a whole query targetlist as a single
 * expression tree.
 *
 * In a SELECT's targetlist, resno should always be equal to the item's ordinal
 * position(counting from 1). However, in an INSERT or UPDATE targetlist, resno
 * represents the attribute number of the destination column for the item; so
 * there may be missing or out-of-order resnos. It is even legal to have duplicated
 * reasons; consider:
 *    UPDATE table SET arraycol[1] = ..., arraycol[2] = ..., ...
 * The two meanings come together in the executor, because the planner transforms
 * INSERT/UPDATE tlists into a normalized form with exactly one entry for each column
 * of the destination table. Before that's happened, however, it is riskly to assume
 * that resno == position. Generally get_tbl_by_resno() should be used rather than
 * list_nth() to fetch tlist entries by resno, and only in SELECT should you assume
 * that resno is a unique identifier.
 *
 * ressortgroupref is used in the representation of ORDER BY, GROUP BY, and
 * DISTINCT items.	Targetlist entries with ressortgroupref=0 are not
 * sort/group items.  If ressortgroupref>0, then this item is an ORDER BY,
 * GROUP BY, and/or DISTINCT target value.	No two entries in a targetlist
 * may have the same nonzero ressortgroupref --- but there is no particular
 * meaning to the nonzero values, except as tags.  (For example, one must
 * not assume that lower ressortgroupref means a more significant sort key.)
 * The order of the associated SortGroupClause lists determine the semantics.
 *
 * resorigtbl/resorigcol identify the source of the column, if it is a
 * simple reference to a column of a base table (or view).	If it is not
 * a simple reference, these fields are zeroes.
 *
 * If resjunk is true then the column is a working column (such as a sort key)
 * that should be removed from the final output of the query.  Resjunk columns
 * must have resnos that cannot duplicate any regular column's resno.  Also
 * note that there are places that assume resjunk columns come after non-junk
 * columns.
 *--------------------
 */
/* _line_1067 */
typedef struct TargetEntry {
  Expr xpr;
  Expr *expr;  /* expression to evaluate */
  AttrNumber resno;  /* attribute number (see notes above) */
  char *resname;  /* name of the column (could be NULL) */
  Index ressortgroupref;  /* nonzero if referenced by a sort/group clause */
  Oid resorigtbl;  /* OID of column's source table */
  AttrNumber resorigcol;  /* column's number in source table */
  bool resjunk;  /* set to true to eliminate the attribute from final target list */
} TargetEntry;
 
/*----------------------------
 * FromExpr - represents a FROM ... WHERE ... construct
 * This is both more flexible than a JoinExpr (it can have any number of 
 * children, including zero) and less so --- we don't need to deal with 
 * aliases and so on. The output column set is implicitly just union of
 * the output of the children.
 *----------------------------
 */
/* _line_1170 */
typedef struct FromExpr {
  NodeTag type;
  List *fromlist;  /* list of join subtrees */
  Node *quals;  /* qualifiers on join, if any */
}FromExpr;
 
#endif
